package org.spring.springboot.app.adv.advexam.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.adv.advcombhard.domain.po.AdvCombHardPO;
import org.spring.springboot.app.adv.advcombinfo.domain.po.AdvCombInfoPO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoDetailVO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoListVO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoSaveInVO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoSearchVO;
import org.spring.springboot.app.adv.advexam.domain.po.AdvExamPO;
import org.spring.springboot.app.adv.advexam.domain.vo.AdvExamDealVO;
import org.spring.springboot.app.adv.advexam.domain.vo.AdvExamQueryVO;
import org.spring.springboot.app.adv.advexam.domain.vo.AdvExamSearchVO;
import org.spring.springboot.app.adv.advexam.service.IAdvExamService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.sys.area.domain.po.SysAreaPO;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(description="广告审核操作接口",tags = ApiIndex.AdvExamController)
@RequestMapping(value = "/api/advExam")
@RestController
@AccessTokenVerify
public class AdvExamController {

    @Autowired
    private IAdvExamService advExamService;

    @ApiOperation(value = "查询分页列表")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<PageInfo<List<AdvExamQueryVO>>> findPageWithAdv(
            @ApiIgnore User user,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "") String title
            ) {
        R result = new R();
        AdvExamSearchVO searchVO = new AdvExamSearchVO();
        searchVO.setTitle(title);
        List<AdvExamQueryVO> list = advExamService.findPageWithAdv(searchVO, pageNum, pageSize);
        result.setData(new PageInfo(list));
        return result;
    }



    @ApiOperation(value = "查询单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<AdvExamQueryVO> find(
            @ApiParam(value = "id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        AdvExamQueryVO vo = advExamService.findWithAdv(id);
        result.setData(vo);
        return result;
    }


    @ApiOperation(value="处理审核")
    @RequestMapping(value = "/deal", method = RequestMethod.PUT)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R deal(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @Valid  @RequestBody AdvExamDealVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advExamService.deal(vo);
    }


    @ApiOperation(value="删除信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R delete(@ApiIgnore User user,@ApiParam(value = "id", required = true)  @PathVariable("id") String id) {
        R result = new R();
        if (StringUtils.isBlank(id)) {
            result.setType(Type.ParamValidateFail);
            result.setMessage("id不能为空");
            return result;
        }
        int code= advExamService.delete(id);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("修改失败");
        }
        return result;
    }

}
